﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using SourFruit.menu;

namespace SourFruit.player
{
    public class KeyboardInput : Input
    {
        private KeyboardState ks, old;

        public KeyboardInput() { }

        /// <summary>
        /// 
        /// </summary>
        public KeyboardInput(Keys _up, Keys _down, Keys _left, Keys _right, Keys _jump, Keys _confirm, Keys _back) : base(_up, _down, _left, _right, _jump, _confirm, _back) { }


        private void setKey(InputActions key, Keys newKey)
        {
            if (ks.IsKeyDown(newKey) && old.IsKeyDown(newKey))
                return;
            switch (key)
            {
                case InputActions.up:
                    UP = newKey;
                    break;
                case InputActions.down:
                    DOWN = newKey;
                    break;
                case InputActions.left:
                    LEFT = newKey;
                    break;
                case InputActions.right:
                    RIGHT = newKey;
                    break;
                case InputActions.jump:
                    JUMP = newKey;
                    break;
                case InputActions.confirm:
                    CONFIRM = newKey;
                    break;
                case InputActions.escape:
                    BACK = newKey;
                    break;
            }
            changingKeys = false;
        }

        public override void Update(MenuWindow menu, Physics physics, GameTime gametime, Boolean gameEnded)
        {
            ks = Keyboard.GetState();
            if (changingKeys)
            {
                if (ks.IsKeyDown(Keys.Escape) && !old.IsKeyDown(Keys.Escape))
                {
                    changingKeys = false;
                }
                else if (ks.GetPressedKeys().Length != 0) setKey(KeyToBeChanged, ks.GetPressedKeys().ElementAt(0));
            }
            else if (!gameEnded && ks.IsKeyDown(Keys.Escape) && !old.IsKeyDown(Keys.Escape))
            {
                menuOpen = !menuOpen;
                menu.CreateEventMenuChanged(menuOpen);
            }
            else if (!menuOpen)
            {
                if (ks.IsKeyDown((Keys)LEFT))
                    physics.MovePlayerX(false, gametime);
                else if (ks.IsKeyDown((Keys)RIGHT))
                    physics.MovePlayerX(true, gametime);
                if (ks.IsKeyDown((Keys)JUMP))
                    physics.Jump();
                physics.UpdateJump(gametime);
                physics.Fall(gametime);
            }
            else if (ks.IsKeyDown((Keys)UP) && !old.IsKeyDown((Keys)UP))
                menu.GoUP();
            else if (ks.IsKeyDown((Keys)DOWN) && !old.IsKeyDown((Keys)DOWN))
                menu.GoDOWN();
            else if (ks.IsKeyDown((Keys)CONFIRM) && !old.IsKeyDown((Keys)CONFIRM))
                menuOpen = menu.Select();
            else
                menu.CreateEventMenuChanged(menuOpen);
            old = ks;
        }

        public override InputActions?[] Update(GameTime gametime, bool gameEnded)
        {
            ks = Keyboard.GetState();
            InputActions?[] input = new InputActions?[2];
            if (changingKeys)
            {
                if (ks.IsKeyDown(Keys.Escape) && !old.IsKeyDown(Keys.Escape))
                {
                    changingKeys = false;
                }
                else if (ks.GetPressedKeys().Length != 0) setKey(KeyToBeChanged, ks.GetPressedKeys().ElementAt(0));
            }
            else if (ks.IsKeyDown(Keys.Escape) && !old.IsKeyDown(Keys.Escape))
            {
                input[0] = InputActions.escape;
            }
            if (ks.IsKeyDown((Keys)LEFT))
                input[0] = InputActions.left;
            else if (ks.IsKeyDown((Keys)RIGHT))
                input[0] = InputActions.right;            
            else if (ks.IsKeyDown((Keys)UP) && !old.IsKeyDown((Keys)UP))
                input[0] = InputActions.up;
            else if (ks.IsKeyDown((Keys)DOWN) && !old.IsKeyDown((Keys)DOWN))
                input[0] = InputActions.down;
            else if (ks.IsKeyDown((Keys)CONFIRM) && !old.IsKeyDown((Keys)CONFIRM))
                input[0] = InputActions.confirm;
            if (ks.IsKeyDown((Keys)JUMP))
                input[1] = InputActions.jump;
            old = ks;
            return input;
        }
    }
}
